/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.Components;

import GUI.Dialogs.AssetChooserDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 *
 * @author David
 */
public class AssetButton extends BButton
{

    private AssetChooserDialog chooser;
    private AssetType assetType;
    private JPopupMenu popupMenu = new JPopupMenu();
    private JMenuItem openItem = new JMenuItem("Show");
    private JMenuItem removeItem = new JMenuItem("Remove");

    private void initPopupMenu()
    {
        popupMenu.add(openItem);
        popupMenu.add(removeItem);
        openItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JDialog tempDialog = new JDialog();
                tempDialog.getContentPane().add(new JLabel(new ImageIcon(chooser.getSelectedAssetName())));
                tempDialog.pack();
                tempDialog.setVisible(true);
            }
        });
        removeItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                chooser.setSelectedAssetName(null);
                AssetButton.this.setText("Select " + assetType.toString());
            }
        });
    }

    /**
     *
     */
    public enum AssetType
    {

        /**
         *
         */
        Texture,
        /**
         *
         */
        Sound,
        /**
         *
         */
        Model,
        /**
         *
         */
        Sky
    }

    /**
     *
     * @param type
     */
    public AssetButton(AssetType type)
    {
        this(type, null);
    }

    /**
     *
     * @param type
     * @param fileName
     */
    public AssetButton(AssetType type, String fileName)
    {
        assetType = type;
        if (fileName == null)
        {
            setText("Select " + assetType.toString());
        } else
        {
            setText(fileName);
        }
        addMouseListener(new MouseListener()
        {
            public void mouseClicked(MouseEvent e)
            {
            }

            public void mousePressed(MouseEvent e)
            {
            }

            public void mouseReleased(MouseEvent e)
            {
                if (e.getButton() == MouseEvent.BUTTON1)
                {
                    chooser = new AssetChooserDialog(assetType,true);
                    setText(chooser.getSelectedAssetName());
                } else
                {
                    if (chooser != null && chooser.getSelectedAssetName() != null)
                    {
                        popupMenu.show(AssetButton.this, e.getPoint().x, e.getPoint().y);
                    }
                }
            }

            public void mouseEntered(MouseEvent e)
            {
            }

            public void mouseExited(MouseEvent e)
            {
            }
        });
        initPopupMenu();
    }

    /**
     *
     * @return
     */
    public AssetType getAssetType()
    {
        return assetType;
    }

    /**
     *
     * @return
     */
    public AssetChooserDialog getAssetChooser()
    {
        return chooser;
    }
}
